const crypto = require('crypto')
const jwt = require('jsonwebtoken')
const {PRIVATE_KEY} = require('./constant')

function isObject(obj){
    return Object.prototype.toString.call(obj)
}

function md5(s){
    // 参数类型需要时String 否则会报错
    return crypto.createHash('md5')
    .update(String(s)).digest('hex')
}

function decode(req){
    let authorization = req.get('Authorization')
    let token = ""
    if(authorization.indexOf('Bearer')>=0){
        token = authorization.replace('Bearer ','')
    }else{
        token = authorization
    }
    return jwt.verify(token,PRIVATE_KEY)

}

module.exports = {
    md5,
    decode,
    isObject
}